package com.qiall;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class Base64Util {
    public static String encode(String text) {
        final BASE64Encoder encoder = new BASE64Encoder();

        byte[] textByte = null;
        try {
            textByte = text.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        final String encodedText = encoder.encode(textByte);
        return encodedText;
    }

    public static String decode(String text) {

        final BASE64Decoder decoder = new BASE64Decoder();
        try {
            return new String(decoder.decodeBuffer(text), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}
